#include "frmmain.h"
#include "ui_frmmain.h"

void frmmain::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


frmmain::frmmain(QWidget *parent) : QMainWindow(parent),   ui(new Ui::frmmain)
{
    ui->setupUi(this);
    Organiser::incoming_dir = "test/test_dir/New";
    Organiser::instance()->init();
    Organiser::instance()->rules;

    populate_table_rules();

    Rule *row;
    row = new Rule();

    QObject::connect(ui->btnRunRules,       SIGNAL(clicked()), this, SLOT(btnRunRules_clicked()));
    QObject::connect(ui->btnIncomingDir,    SIGNAL(clicked()), this, SLOT(btnIncomingDir_clicked()));
    QObject::connect(ui->mnuNewRule,        SIGNAL(triggered()), this, SLOT(mnuNewRule_clicked()));
    QObject::connect(ui->tblRules,          SIGNAL(doubleClicked(QModelIndex)), this, SLOT(tblRulesClicked_showRule(QModelIndex)));
    QObject::connect(ui->btnExecute,        SIGNAL(clicked()), this, SLOT(btnExecute_clicked()));
    QObject::connect(ui->btnListRefresh,    SIGNAL(clicked()), this, SLOT(btnListRefresh_clicked()));

    ui->tabWidget->setTabText(0, "Local");
    ui->tabWidget->setTabText(1, "Remote Rules");

    // registers meta types
    qRegisterMetaType<Rule>("Rule");

    Rule r("new rule");
    insert_new_rule(r);

}

frmmain::~frmmain()
{
    delete ui;
}
void frmmain::btnExecute_clicked()
{
    Organiser::instance()->execute_matches();
}

void frmmain::btnRunRules_clicked()
{    
    Organiser::instance()->run_rules();
    QMatchedPathModel *matched_model = new QMatchedPathModel(&Organiser::instance()->rule_matches);
    ui->tblMatches->setModel(matched_model);
    ui->tblMatches->show();
}

void frmmain::mnuNewRule_clicked()
{
    frmRule *ruleNew = new frmRule(this);
    ruleNew->show();
}

void frmmain::btnIncomingDir_clicked()
{
    QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), "/home/krone");
    if (!path.isNull())
    {
        ui->txtIncomingDir->setText(path);
        Organiser::incoming_dir = path.toStdString();
    }
}

void frmmain::btnListRefresh_clicked()
{
    Organiser::cb_ptr_func = cb_populate_rule_groups;
    Organiser::instance()->get_rule_groups(this);
}

void frmmain::tblRulesClicked_showRule(QModelIndex ind)
{
    vector<Rule> rules = Organiser::instance()->rules;
    Rule rule;

    int c = 0;
    for(vector<Rule>::const_iterator i = rules.begin(); i!=rules.end(); ++i)
    {
         if(c==ind.row())
         {
            rule = (Rule)*i;
            frmRule *ruleNew = new frmRule(&rule);
            ruleNew->show();
            break;
         }
         c++;
    }
}

void frmmain::cb_populate_rule_groups()
{
    frmmain *gui = (frmmain*)Organiser::instance()->cb_object;    

    QRuleGroupModel *rg_model = new QRuleGroupModel(&Organiser::instance()->rule_groups);
    gui->ui->tblRuleGroups->setModel(rg_model);
    gui->ui->tblRuleGroups->show();
}

void frmmain::populate_table_rules()
{
    QRuleModel *rule_model = new QRuleModel(&Organiser::instance()->rules);
    ui->tblRules->setModel(rule_model);
    ui->tblRules->show();
    //cout << Organiser::instance()->rules[0].rule_name << endl;
}

void frmmain::insert_new_rule(Rule rule) const
{
    // frmrule will call this method to insert a new rule    
    Organiser::instance()->insert<Rule>(rule);
}

